package ui.swing.jt.ch9;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

public class JEditorPane4{

	public static void main(String[] args){
		
		final JEditorPane editPane =new JTextPane();
		try{
		  // File thef = new File ("docs/JEditorPane_1.html");
		  // String str  = JEditorPane4.class.getResource("docs/JEditorPane_1.html").getFile();
		  String str  = JEditorPane4.class.getResource("docs/Table.html").getFile();
	    	 // String str = thef.getAbsolutePath();
	    	  str = "file:"+str;
	    	  // editPane = new JTextPane();
	    	  editPane.setPage(str);
	    	  editPane.getDocument().addDocumentListener(new DocumentListener() {
				
				public void removeUpdate(DocumentEvent e) {
					System.out.println(editPane.getText());
				}
				
				public void insertUpdate(DocumentEvent e) {
					System.out.println(editPane.getText());
				}
				
				public void changedUpdate(DocumentEvent e) {
					System.out.println(editPane.getText());
				}
			});
	    	}
		catch(IOException ioe){
		  ioe.printStackTrace(System.err);
		  System.exit(0);
		}
		editPane.setEditable(true);
		
		final JEditorPane thePane = editPane;
		editPane.addHyperlinkListener(new HyperlinkListener(){
		  public void hyperlinkUpdate(HyperlinkEvent hle){
		    try{
		      if (hle.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
		        thePane.setPage(hle.getURL());
		    }
		    catch(IOException ioe){
		      ioe.printStackTrace(System.err);
		    }
		  }
		});
		
		JFrame f = new JFrame("JEditorPane4");
		f.setContentPane(new JScrollPane(editPane));
		f.setSize(200,250);
    		f.show(); 
    		f.addWindowListener(new WindowAdapter() {            
    		  public void windowClosing(WindowEvent e) {                    
    		 	  System.exit(0);            
    		  }        
    		});    
	}//end of main()
}//end of class JEditPane1